package org.cqipc.edu.controller;

import org.cqipc.edu.entity.User;
import org.cqipc.edu.service.UserService;
import org.cqipc.edu.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Description: 用户控制器
 * @ClassNmae: UserController
 * @Author: HeYue
 * @DateTime: 2020-10-30 20:48
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/loginCheck")
    public Object loginCheck(LoginVO loginVO, HttpSession session) {
        if (session.getAttribute("loginInfo") != null) {
            return "cf";
        } else {
            return userService.login(loginVO.getUserName(), loginVO.getUserPassword(), session);
        }
    }


    @GetMapping("/loginOut")
    public Object loginOut(HttpSession session) {
        session.removeAttribute("loginInfo");
        return "退出成功!";
    }


    @GetMapping("/findLoginInfo")
    public Object findLoginInfo(HttpSession session) {
        if (session.getAttribute("loginInfo") != null) {
            return ((User) session.getAttribute("loginInfo")).getUserName();
        }
        return "null";
    }


    @PostMapping("/registerUser")
    public Object register(LoginVO loginVO, HttpSession session) {
        return userService.addUserInfo(loginVO, session);
    }

}
